﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GzipManagerPro.UI.Utility;
using GzipManagerPro.IO;
using System.IO;
using GzipManagerPro.UI.Dialogs;

namespace GzipManagerPro.UI.Controls.Preview
{
    public partial class PreviewBase : UserControl, ICommandProvider
    {
        public string FileName { get; set; }
        public PreviewBase()
        {
            InitializeComponent();
        }

        public virtual void GeneratePreview(string path)
        {
            this.FileName = path;
        }

        public virtual Command[] GetCommands(CustomFileSystemInfo info)
        {
            List<Command> commands = new List<Command>();

            commands.Add(new Command()
            {
                Title = "Archive",
                Image = GzipManagerPro.UI.Properties.Resources.ExtractAll,
                Method = o => AddToArchive(o, true)
            });

            commands.Add(new Command()
            {
                Title = "Archive to...",
                Image = GzipManagerPro.UI.Properties.Resources.ExtractAll,
                Method = o => AddToArchive(o, false)
            });


            return commands.ToArray();
        }

        private bool AddToArchive(CustomFileSystemInfo info, bool useFilePath)
        {

            Archive archive = new Archive();
            archive.FileName = Path.Combine(Path.GetDirectoryName(info.FullName),
                info.Name);

            archive.FileName += Archive.Extension;

            if (!useFilePath)
            {

                SaveFileDialog saveDialog = new SaveFileDialog()
                {
                    Filter = Archive.ExtensionFilter,
                    FileName = archive.FileName
                };

                if (saveDialog.ShowDialog() == DialogResult.Cancel)
                    return false;

                archive.FileName = saveDialog.FileName;
            }

            ArchiveDialog dialog = new ArchiveDialog();
            if (dialog.ShowDialog() == DialogResult.Cancel)
                return false;

            ProgressDialog Progress = new ProgressDialog()
            {
                Text = "Creating archive..."
            };
            Progress.Show();
            Application.DoEvents();

            archive.StoryBoard = dialog.StoryBoardValue;
            archive.ArchiveMessage += (s, e) => Progress.TranslateMessage(e);

            if (!string.IsNullOrEmpty(dialog.PasswordValue))
                archive.SetPassword(dialog.PasswordValue);

            if (info.IsDirectory)
                archive.AddDirectory(info.FullName);
            else
                archive.AddFile(info.FullName);



            archive.SaveToFile();
            Application.DoEvents();
            Progress.Close();
            Application.DoEvents();

            return true;
        }
    }
}
